package com.py.credit.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.py.credit.dao.ColumnDao;
import com.py.credit.dao.ManualAssociateDao;
import com.py.credit.dao.RuleCheckingDao;
import com.py.credit.dao.TableDao;
import com.py.credit.entity.Column;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.Table;
import com.py.credit.service.ManualAssociateService;
import com.py.credit.utils.SQLBuilder;
import com.py.framework.core.common.Page;

@Service
public class ManualAssociateServiceImpl implements ManualAssociateService {

	@Autowired
	private ManualAssociateDao manualAssociateDao;

	@Autowired
	private ColumnDao columnDao;

	@Autowired
	TableDao tableDao;

	@Autowired
	RuleCheckingDao ruleCheckingDao;

	@Override
	public List<Map<String, Object>> getByConditionForPage(List<String> columns, String[] additionalColumns,
			String conditionStr, int rootId, Page page, SysUser userInfo) {
		List<String> conditions = SQLBuilder.createConditionSql(conditionStr);
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		if (rootId == 100000) {
			data = manualAssociateDao.selectFRByConditionForPage(columns, additionalColumns, conditions,
					userInfo.getOfficeId(), userInfo.getUserType(), userInfo.getAreaId(), page);
		} else if (rootId == 100001) {
			data = manualAssociateDao.selectZRRByConditionForPage(columns, additionalColumns, conditions,
					userInfo.getOfficeId(), userInfo.getUserType(), userInfo.getAreaId(), page);
		}
		return data;

	}

	@Override
	public Map<String, Object> selectOne(int tableId, int dataId) {
		Table table = tableDao.selectByPrimaryKey(tableId);
		List<Column> columns = columnDao.selectListByTableId(tableId);
		String tableName = "t_" + table.getResourceId();
		List<Map<String, Object>> data = manualAssociateDao.selectOne(tableName, dataId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("columns", columns);
		map.put("data", data);
		return map;
	}

	@Override
	public List<Map<String, Object>> selectAssociate(int rootId, int tableId, int dataId) {
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		Table table = tableDao.selectByPrimaryKey(tableId);
		String tableName = "t_" + table.getResourceId();
		if (rootId == 100000) {
			data = manualAssociateDao.selectFrAssociate(tableName, dataId);
		} else if (rootId == 100001) {
			data = manualAssociateDao.selectZrrAssociate(tableName, dataId);
		}
		return data;
	}

	@Override
	public List<Map<String, Object>> seachDataByConditionForPage(List<String> columns, String[] additionalColumns,
			String conditionStr, int rootId, String A000001, String A000002,String A000003, Page page) {
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		List<String> conditions = SQLBuilder.createConditionSql(conditionStr);
		StringBuffer sql2 = new StringBuffer(1000);
		if (rootId == 100000) {
			sql2.append("select ");
			sql2.append(" tybm,A000001,A000002,A000003,A000004,A000005,A000006 ");
			sql2.append(" from ").append(" t_zoo_subject_unique_100000 ");
			if (CollectionUtils.isEmpty(conditions)) {
				sql2.append(" where ").append(" MATCH (A000001,A000002,A000003) AGAINST ");
				sql2.append("(");
				sql2.append("\'" + A000001);
				sql2.append(" ").append(A000002);
				sql2.append(" ").append(A000003).append("\'");
				sql2.append(") ");
			} else {
				sql2.append(" where 1=1 ");
				for (String c : conditions) {
					sql2.append(" and ").append(c);
				}
			}
			// limit m,n
			sql2.append(" limit 0,30 ");
			data = manualAssociateDao.seachFrDataByConditionForPage(columns, additionalColumns, conditions,
					sql2.toString(), page);
		} else if (rootId == 100001) {
			sql2.append("select ");
			sql2.append("tybm,B000001,B000002,B000003 ");
			sql2.append(" from ").append(" t_zoo_subject_unique_100001 ");
			if (CollectionUtils.isEmpty(conditions)) {
				sql2.append(" where ").append(" MATCH (B000002) AGAINST ");
				sql2.append("(");
				sql2.append("\'" + A000001).append("\'");
				sql2.append(") ");
			} else {
				sql2.append(" where 1=1 ");
				for (String c : conditions) {
					sql2.append(" and ").append(c);
				}
			}
			sql2.append(" limit 0,30 ");
			data = manualAssociateDao.selectZrrDataByConditionForPage(columns, additionalColumns, conditions,
					sql2.toString(), page);
		}
		return data;
	}

	@Override
	public void updateData(int rootId, String tybm, int tableId, int dataId) {
		Table table = tableDao.selectByPrimaryKey(tableId);
		String tableName = "t_" + table.getResourceId();

		// 更新数据表中的数据
		manualAssociateDao.updateData(tybm, tableName, dataId);
		manualAssociateDao.lessWaitCount(tableName, dataId);

		// 将更新后的数据表里的数据与tybm 关系 插入t_dc_tybm_cube表中
		// 删除关联
		// ruleCheckingDao.addTableAndTybm(tybm, table.getResourceId());

		if (rootId == 100000) {
			// 从待关联表中 将改数据状态修改为删除
			manualAssociateDao.deleteFrData(tableId, dataId);
		} else if (rootId == 100001) {
			// 从待关联表中 将改数据状态修改为删除
			manualAssociateDao.deleteZrrData(tableId, dataId);
		}

	}
}
